/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.dinner.service.order;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.CrudService;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.dinner.entity.order.ResOrder;
import com.thinkgem.jeesite.modules.dinner.dao.order.ResOrderDao;
import com.thinkgem.jeesite.modules.dinner.entity.order.ResOrderDetail;
import com.thinkgem.jeesite.modules.dinner.dao.order.ResOrderDetailDao;

/**
 * 订单管理Service
 * @author dai
 * @version 2018-11-26
 */
@Service
@Transactional(readOnly = true)
public class ResOrderService extends CrudService<ResOrderDao, ResOrder> {

	@Autowired
	private ResOrderDetailDao resOrderDetailDao;
	
	public ResOrder get(String id) {
		ResOrder resOrder = super.get(id);
		resOrder.setResOrderDetailList(resOrderDetailDao.findList(new ResOrderDetail(resOrder)));
		return resOrder;
	}
	
	public List<ResOrder> findList(ResOrder resOrder) {
		return super.findList(resOrder);
	}
	
	public Page<ResOrder> findPage(Page<ResOrder> page, ResOrder resOrder) {
		return super.findPage(page, resOrder);
	}
	
	@Transactional(readOnly = false)
	public void save(ResOrder resOrder) {
		super.save(resOrder);
		for (ResOrderDetail resOrderDetail : resOrder.getResOrderDetailList()){
			if (resOrderDetail.getId() == null){
				continue;
			}
			if (ResOrderDetail.DEL_FLAG_NORMAL.equals(resOrderDetail.getDelFlag())){
				if (StringUtils.isBlank(resOrderDetail.getId())){
					resOrderDetail.setResOrder(resOrder);
					resOrderDetail.preInsert();
					resOrderDetailDao.insert(resOrderDetail);
				}else{
					resOrderDetail.preUpdate();
					resOrderDetailDao.update(resOrderDetail);
				}
			}else{
				resOrderDetailDao.delete(resOrderDetail);
			}
		}
	}
	
	@Transactional(readOnly = false)
	public void delete(ResOrder resOrder) {
		super.delete(resOrder);
		resOrderDetailDao.delete(new ResOrderDetail(resOrder));
	}
	
}